package com.heba.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heba.bean.user.UserObject;
import com.heba.greenmulu.R;
import com.heba.utils.CommonUtils;
import com.heba.utils.DensityUtils;
import com.heba.utils.GlideUtil;
import com.heba.utils.ToastShow;


/**
 * 自定义显示对话框
 * <p>
 * Created by shenghao on 2017/8/3.
 */
public class CustomDialogView {

    private OnCustomDialogClickListener dialogClickListener;
    private OnCustomBindEmailClickListener bindEmailClickListener;
    private OnSelectEditDialogClickListener selectEditDialogClickListener;
    private OnSendMessageClickListener sendMessageClickListener;

    public CustomDialogView(OnSelectEditDialogClickListener selectEditDialogClickListener) {
        this.selectEditDialogClickListener = selectEditDialogClickListener;
    }

    public CustomDialogView(OnCustomBindEmailClickListener onCustomBindEmailClickListener) {
        this.bindEmailClickListener = onCustomBindEmailClickListener;
    }

    public CustomDialogView(OnCustomDialogClickListener dialogClickListener) {
        this.dialogClickListener = dialogClickListener;
    }

    public CustomDialogView(OnSendMessageClickListener sendMessageClickListener) {
        this.sendMessageClickListener = sendMessageClickListener;
    }

    /**
     * 自定义对话框显示
     *
     * @param context
     * @param title
     * @param content
     */
    public void showDialog(Context context, String title, String content) {
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        View layout = LayoutInflater.from(
                context).inflate(R.layout.dialog_view, null);
        TextView txt_title = (TextView) layout.findViewById(R.id.txt_title);
        TextView txt_content = (TextView) layout.findViewById(R.id.txt_content);
        TextView txt_yes = (TextView) layout.findViewById(R.id.txt_yes);
        TextView txt_no = (TextView) layout.findViewById(R.id.txt_no);

        txt_title.setText(title);
        txt_content.setText(content);

        if (title.isEmpty())
            txt_title.setVisibility(View.GONE);
        if (content.isEmpty())
            txt_content.setVisibility(View.GONE);

        txt_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (dialogClickListener != null) {
                    dialogClickListener.onYesClick();
                }
            }
        });

        txt_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (dialogClickListener != null) {
                    dialogClickListener.onNoClick();
                }
            }
        });

        //设置它的ContentView
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        //设置对话框宽高
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        params.width = 4 * DensityUtils.getWidth(context) / 5;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置对话框位置
//        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        dialog.getWindow().setAttributes(params);
    }

    /**
     * 自定义对话框显示
     *
     * @param context
     * @param title
     * @param content
     * @param yes
     * @param no
     */
    public void showDialogWithButton(Context context, String title, String content, String yes, String no) {
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        View layout = LayoutInflater.from(
                context).inflate(R.layout.dialog_view, null);
        TextView txt_title = layout.findViewById(R.id.txt_title);
        TextView txt_content = layout.findViewById(R.id.txt_content);
        View view_line = layout.findViewById(R.id.view_line);
        TextView txt_yes = layout.findViewById(R.id.txt_yes);
        TextView txt_no = layout.findViewById(R.id.txt_no);

        txt_title.setText(title);
        txt_content.setText(content);
        txt_yes.setText(yes);
        txt_no.setText(no);

        if (title.isEmpty())
            txt_title.setVisibility(View.GONE);
        if (content.isEmpty())
            txt_content.setVisibility(View.GONE);
        if (yes.isEmpty()) {
            view_line.setVisibility(View.GONE);
            txt_yes.setVisibility(View.GONE);
        }
        if (no.isEmpty()) {
            view_line.setVisibility(View.GONE);
            txt_no.setVisibility(View.GONE);
        }

        txt_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (dialogClickListener != null) {
                    dialogClickListener.onYesClick();
                }
            }
        });

        txt_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (dialogClickListener != null) {
                    dialogClickListener.onNoClick();
                }
            }
        });

        //设置它的ContentView
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        //设置对话框宽高
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        params.width = 4 * DensityUtils.getWidth(context) / 5;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置对话框位置
//        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        dialog.getWindow().setAttributes(params);
    }

    /**
     * 绑定电子邮箱对话框
     *
     * @param context
     */
    public void showDialogBindEmail(Context context) {
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        View layout = LayoutInflater.from(
                context).inflate(R.layout.dialog_email_view, null);
        EditText edit_email = layout.findViewById(R.id.edit_email);
        TextView txt_yes = layout.findViewById(R.id.txt_yes);
        TextView txt_no = layout.findViewById(R.id.txt_no);

        txt_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CommonUtils.isEmail(edit_email.getText().toString())) {
                    ToastShow.displayToast(context, context.getResources().getString(R.string.input_email_format));
                    return;
                }

                if (bindEmailClickListener != null) {
                    bindEmailClickListener.onYesClick(edit_email.getText().toString());
                }

                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

        txt_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (bindEmailClickListener != null) {
                    bindEmailClickListener.onNoClick();
                }
            }
        });

        //设置它的ContentView
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        //设置对话框宽高
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        params.width = 4 * DensityUtils.getWidth(context) / 5;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置对话框位置
//        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        dialog.getWindow().setAttributes(params);
    }

    /**
     * 自定义对话框 填写原因(checkbox、edittext)
     *
     * @param context
     */
    public void showDialogWithSelectAndEditText(Context context) {
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        View layout = LayoutInflater.from(
                context).inflate(R.layout.dialog_select_edit_view, null);
        TextView txt_title = layout.findViewById(R.id.txt_title);
        LinearLayout linear_choice1 = layout.findViewById(R.id.linear_choice1);
        LinearLayout linear_choice2 = layout.findViewById(R.id.linear_choice2);
        LinearLayout linear_choice3 = layout.findViewById(R.id.linear_choice3);
        LinearLayout linear_choice4 = layout.findViewById(R.id.linear_choice4);
        EditText edit_content = layout.findViewById(R.id.edit_content);
        TextView txt_yes = layout.findViewById(R.id.txt_yes);
        TextView txt_no = layout.findViewById(R.id.txt_no);

        linear_choice1.setOnClickListener(v -> {
            linear_choice1.setSelected(!v.isSelected());
        });

        linear_choice2.setOnClickListener(v -> {
            linear_choice2.setSelected(!v.isSelected());
        });

        linear_choice3.setOnClickListener(v -> {
            linear_choice3.setSelected(!v.isSelected());
        });

        linear_choice4.setOnClickListener(v -> {
            linear_choice4.setSelected(!v.isSelected());
        });

        txt_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!linear_choice1.isSelected() && !linear_choice2.isSelected() && !linear_choice3.isSelected() && !linear_choice4.isSelected()) {
                    ToastShow.displayToast(context, context.getResources().getString(R.string.please_select_reason));
                    return;
                }

                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (selectEditDialogClickListener != null) {
                    selectEditDialogClickListener.onYesClick(linear_choice1.isSelected(), linear_choice2.isSelected(),
                            linear_choice3.isSelected(), linear_choice4.isSelected(), edit_content.getText().toString());
                }
            }
        });

        txt_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (selectEditDialogClickListener != null) {
                    selectEditDialogClickListener.onNoClick();
                }
            }
        });

        //设置它的ContentView
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        //设置对话框宽高
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        params.width = 4 * DensityUtils.getWidth(context) / 5;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置对话框位置
//        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        dialog.getWindow().setAttributes(params);
    }

    /**
     * 傳送信息對話框
     *
     * @param context
     */
    public void showDialogSendMessage(Context context, String title, UserObject object) {
        //此处直接new一个Dialog对象出来，在实例化的时候传入主题
        final Dialog dialog = new Dialog(context, R.style.MyDialog);
        View layout = LayoutInflater.from(
                context).inflate(R.layout.dialog_send_message_view, null);
        TextView txt_title = layout.findViewById(R.id.txt_title);
        ImageView iv_avatar = layout.findViewById(R.id.iv_avatar);
        TextView tv_name = layout.findViewById(R.id.tv_name);
        TextView txt_cancel = layout.findViewById(R.id.txt_cancel);
        TextView txt_send = layout.findViewById(R.id.txt_send);

        txt_title.setText(title);
        GlideUtil.loadImageWithPlaceHolderID(context, object.getIcon(), iv_avatar, R.drawable.icon_head_portrait);
        tv_name.setText(object.getAccount());

        /**
         * 傳送消息
         */
        txt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendMessageClickListener != null) {
                    sendMessageClickListener.onSendClick();
                }

                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });

        /**
         * 取消
         */
        txt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }

                if (sendMessageClickListener != null) {
                    sendMessageClickListener.onCancelClick();
                }
            }
        });

        //设置它的ContentView
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        //设置对话框宽高
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        params.width = 4 * DensityUtils.getWidth(context) / 5;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置对话框位置
//        dialog.getWindow().setGravity(Gravity.LEFT | Gravity.BOTTOM);
        dialog.getWindow().setAttributes(params);
    }

    /**
     * 回调接口
     */
    public interface OnCustomDialogClickListener {

        void onYesClick();

        void onNoClick();
    }

    /**
     * 绑定邮箱对话框回调接口
     */
    public interface OnCustomBindEmailClickListener {

        void onYesClick(String email);

        void onNoClick();
    }

    /**
     * 填写原因对话框回调接口
     */
    public interface OnSelectEditDialogClickListener {

        void onYesClick(boolean select1, boolean select2, boolean select3, boolean select4, String content);

        void onNoClick();
    }

    /**
     * 傳送信息回調接口
     */
    public interface OnSendMessageClickListener {

        void onSendClick();

        void onCancelClick();
    }

}
